package com.xxxx.worm;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import java.io.IOException;

public class GainMessage {

    //get
    public static WormObj gainMessageByNetwork(String url) {
        //声明http对象
         HttpClient httpClient = new HttpClient();
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);//设置超时时间
        //get请求
        GetMethod getMethod = new GetMethod(url);
        getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT,5000);//设置get请求超时时间

        try {
            int statusCd = httpClient.executeMethod(getMethod);//状态码
            if (statusCd != HttpStatus.SC_OK) {
                System.err.print("访问未成功："+getMethod.getStatusLine());
            }
            //访问成功  获取字符
            byte[] responseBody = getMethod.getResponseBody();
            //获取到返回类型
            Header responseHeader = getMethod.getResponseHeader("Content-Type");
            String value = responseHeader.getValue();

            System.out.print("返回类型："+value);
            //传值到封装对象中
            WormObj wormObj = new WormObj(url, value, responseBody);
            return wormObj;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;


    }



}
